﻿using System;

namespace Core.Info
{
    /// <summary>
    /// 应用时间,取代DateTime.Now的存在，该时间基本上与服务器时间一致
    /// </summary>
    public struct AppTime
    {
        /// <summary>
        /// 设置时间当前时间是起始时间
        /// </summary>
        private static DateTime _startTime = DateTime.Now;

        /// <summary>
        /// 传入的时间与调用时SetAppDateTime相差的描述
        /// </summary>
        private static int _startMilSenc = 0;

        /// <summary>
        /// 应用时间
        /// </summary>
        static AppTime()
        {
            SetAppDateTime(DateTime.Now);
        }

        /// <summary>
        /// 设置应用日期时间
        /// </summary>
        /// <param name="dt"></param>
        public static void SetAppDateTime(DateTime dt)
        {
            _startTime = dt;
            _startMilSenc = Environment.TickCount;
        }

        /// <summary>
        /// App当前时间
        /// </summary>
        public static DateTime Now => _startTime.AddMilliseconds(Environment.TickCount - _startMilSenc);

        /// <summary>
        /// App当前时间的后一天
        /// </summary>
        public static DateTime NowTomorrow => Now.AddDays(1);


        /// <summary>
        /// App当前时间的前一天
        /// </summary>
        public static DateTime NowYesterday => Now.AddDays(-1);

        /// <summary>
        /// App当前Uninx时间戳
        /// </summary>
        public static long NowUnixTime => (Now.ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000000;

        /// <summary>
        /// App当前Uninx时间戳(毫秒级)
        /// </summary>
        public static long NowUnixTimeMs => (Now.ToUniversalTime().Ticks - new DateTime(1970, 1, 1).Ticks) / 10000;
    }
}
